import type { NuxtAxiosInstance } from "@nuxtjs/axios"



export default ($axios: NuxtAxiosInstance) => {
    return {
        /**
         * 插入文章
         * @returns 
         */
        insertArticle: (data: any) => $axios({
            url: '/admin/articles',
            method: 'post',
            data,
            headers: {
                ContentType: 'application/json;'
            }
        }),


        /**
         * 更新文章文章
         * @returns 
         */
        updateArticle: (data: any) => $axios({
            url: '/admin/articles',
            method: 'put',
            data,
            headers: {
                ContentType: 'application/json;'
            }
        }),


        /**
         * 获取所有的文件夹
         * @returns 
         */
         getDirs: () => $axios({
            url: '/admin/cos/dirs',
            method: 'get'
        }),

        /**
         * 获取所有的资源通过文件夹名称
         * @returns 
         */
        getResByKey: (params: { dirKey: string }) => $axios({
            url: '/admin/cos/resByKey',
            method: 'get',
            params
        }),

         /**
         * 向指定文件目录下上传单个文件
         * @returns 
         */
         uploadFile: (data: { file: File, dirKey: string }) => {
            const formData = new FormData()
            formData.append('dirKey', data.dirKey)
            formData.append('file', data.file)
            return $axios({
                url: '/admin/cos/uploadFile',
                method: 'post',
                data: formData,
                headers: {
                    ContentType: 'multipart/form-data;'
                }
            })
        },


         /**
         * 删除文件夹或文件通过key
         * @returns 
         */
         deleteByKey: (params: { key: string }) => {
            return $axios({
                url: '/admin/cos/resByKey',
                method: 'delete',
                params,
            })
        },

        /**
         * 分页查询文章列表
         * @returns 
         */
        getArticles: (params: { [key: string]: any }) => {
            return $axios({
                url: '/admin/articles',
                method: 'get',
                params,
            })
        },

        /**
         * 查询标签列表
         * @returns 
         */
        getTags: (params: { [key: string]: any }) => {
            return $axios({
                url: '/admin/tags',
                method: 'get',
                params,
            })
        },

         /**
         * 查询标签列表
         * @returns 
         */
         getArticleById: (params: { id: number | string }) => {
            return $axios({
                url: `/admin/article/${params.id}`,
                method: 'get'
            })
        },

        /**
         * 账号密码登录
         * @returns 
         */
        login: (data: any) => {
            return $axios({
                url: '/auth/login',
                method: 'post',
                data
            })
        },
    }
}